package com.jiuzhe.commercial.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.bean.HouseListBean;
import com.jiuzhe.commercial.view.GlideRoundTransform;

import java.util.List;

/**
 * Created by huanghao on 2018/4/27.
 */

public class HouseStayListAdapter extends RecyclerView.Adapter<HouseStayListAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<HouseListBean> list;
    private OnItemClickListener mOnItemClickListener = null;

    public HouseStayListAdapter(List<HouseListBean> list) {
        this.list = list;
    }

    public  interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_house_stay_item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        v.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final HouseListBean houseListBean = list.get(position);
        holder.itemView.setTag(position);
        holder.tvRoomName.setText(houseListBean.getSkuName());
        holder.tvRoomAddress.setText(houseListBean.getAddress());
        holder.tvRoomPrice.setText(houseListBean.getRoomPrice() + "元/每晚");
        if (houseListBean.getRoomStatus().equals("1") ){
            holder.tvRoomState.setText("已上架");
        }else {
            holder.tvRoomState.setText("已下架");
        }
        List<String> list = houseListBean.getImgUrlList();
        holder.cardView.setRadius(8);//设置图片圆角的半径大小

        holder.cardView.setCardElevation(8);//设置阴影部分大小

        holder.cardView.setContentPadding(5,5,5,5);//设置图片距离阴影大小

        if (list!=null && list.size()>0) {
            RequestOptions options = new RequestOptions()
                    .dontAnimate()
                    .error(R.mipmap.image)
                    .transform(new GlideRoundTransform(context,5))
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);

            Glide.with(context)
                    .load(list.get(0))
                    .apply(options)
                    .into(holder.mIvImg);
        }else {
            holder.mIvImg.setImageResource(R.mipmap.image);
        }
    }


    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mIvImg;
        TextView tvRoomName;
        TextView tvRoomPrice;
        TextView tvRoomAddress;
        TextView tvRoomState;
        CardView cardView;

        public ViewHolder(View v) {
            super(v);
            mIvImg = v.findViewById(R.id.iv_img);
            tvRoomAddress = v.findViewById(R.id.tv_adress);
            tvRoomPrice = v.findViewById(R.id.tv_roomPrice);
            tvRoomName = v.findViewById(R.id.tv_roomName);
            cardView = v.findViewById(R.id.cardView);
            tvRoomState = v.findViewById(R.id.tv_roomState);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }



}
